package com.platform.api.bzproject.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.platform.api.bzproject.dao.DistributionProfitMapper;
import com.platform.api.bzproject.model.BalanceRecord;
import com.platform.api.bzproject.model.DistributionProfit;
import com.platform.api.bzproject.service.IDistributionProfitService;
import com.platform.common.core.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 分销收益表 服务实现类
 * </p>
 *
 * @author guoshao
 * @since 2021-12-10
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DistributionProfitServiceImpl extends ServiceImpl<DistributionProfitMapper, DistributionProfit> implements IDistributionProfitService {

    @Autowired
    private DistributionProfitMapper distributionProfitMapper;

    @Override
    public PageInfo<DistributionProfit> pageInfo(DistributionProfit distributionProfit, Integer offset) {

        Integer pageNum = 1;
        if(offset > 0){
            pageNum = (offset / Constant.PAGESIZE) + 1;
        }

        PageHelper.startPage(pageNum, Constant.PAGESIZE);

        PageInfo<DistributionProfit> pageInfo = new PageInfo<>(distributionProfitMapper.selectDistributionProfitList(distributionProfit));
        return pageInfo;
    }

    //查询广告收益和分销收益
    @Override
    public PageInfo<DistributionProfit> selectAllProfitList(DistributionProfit distributionProfit, Integer offset) {
        Integer pageNum = 1;
        if(offset > 0){
            pageNum = (offset / Constant.PAGESIZE) + 1;
        }

        PageHelper.startPage(pageNum, Constant.PAGESIZE);

        PageInfo<DistributionProfit> pageInfo = new PageInfo<>(distributionProfitMapper.selectAllProfitList(distributionProfit));
        return pageInfo;
    }

    @Override
    public BigDecimal selectSumMoney(DistributionProfit profit) {
        return distributionProfitMapper.selectSumMoney(profit);
    }


    /**
     * 删除用户分销数据
     * @param userId  所属用户ID
     * @param souUserId 来源用户ID
     * @param count 删除行数
     */
    @Override
    public void delRandId(String userId,String souUserId, int count) {
        Map<String,Object> map = new HashMap<>(6);
        map.put("userId",userId);
        map.put("count",count);
        map.put("souUserId",souUserId);
        List<Long> randId = distributionProfitMapper.getRandId(map);
        if(randId.size() > 0){
            distributionProfitMapper.deleteBatchIds(randId);
        }

    }
}
